/*
 * @Author: moxunjingmu
 * @Date: 2025-03-03 22:06:08
 * @Description: 
 */
import { Question, Category, Difficulty } from "@/types/question";
import questionsData from "./data/index";

const generateId = (category: string, index: number): string =>
  `${category}-${index.toString().padStart(3, "0")}`;

const createQuestions = (
  category: Category,
  questions: Array<Omit<Question, "id" | "category">>
): Question[] => {
  return questions.map((q, index) => ({
    ...q,
    id: generateId(category, index + 1),
    category,
  }));
};

// 模拟API请求延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

// 异步获取所有问题
const fetchQuestions = async (): Promise<Question[]> => {
  await delay(500); // 模拟网络延迟
  const allQuestions: Question[] = [];
  
  Object.entries(questionsData).forEach(([category, questions]) => {
    const categoryQuestions = createQuestions(category as Category, questions as Array<Omit<Question, "id" | "category">>);
    allQuestions.push(...categoryQuestions);
  });
  
  return allQuestions;
};

// 缓存获取的问题
let cachedQuestions: Question[] | null = null;

// 获取所有问题
export const getAllQuestions = async (): Promise<Question[]> => {
  if (!cachedQuestions) {
    cachedQuestions = await fetchQuestions();
  }
  return cachedQuestions;
};

// 根据分类获取问题
export const getQuestionsByCategory = async (category: Category): Promise<Question[]> => {
  const questions = await getAllQuestions();
  return questions.filter((q) => q.category === category);
};

// 根据ID获取问题
export const getQuestionById = async (id: string): Promise<Question | undefined> => {
  const questions = await getAllQuestions();
  return questions.find((q) => q.id === id);
};

// 获取随机问题
export const getRandomQuestions = async (
  count: number,
  category?: Category,
  difficulty?: Difficulty
): Promise<Question[]> => {
  let questions = await getAllQuestions();
  
  if (category) {
    questions = questions.filter((q) => q.category === category);
  }
  if (difficulty) {
    questions = questions.filter((q) => q.difficulty === difficulty);
  }
  
  return questions
    .sort(() => 0.5 - Math.random())
    .slice(0, Math.min(count, questions.length));
};

// 分类数据
export const categories: { id: Category; name: string; icon: string }[] = [
  { id: "javascript", name: "JavaScript", icon: "code" },
  { id: "typescript", name: "TypeScript", icon: "type" },
  { id: "vue", name: "Vue.js", icon: "layers" },
  { id: "react", name: "React", icon: "atom" },
  { id: "node", name: "Node.js", icon: "server" },
  { id: "css", name: "CSS", icon: "palette" },
  { id: "html", name: "HTML", icon: "file-code" },
  { id: "performance", name: "性能优化", icon: "gauge" },
  { id: "design-patterns", name: "设计模式", icon: "component" },
  { id: "engineering", name: "工程化", icon: "cog" },
  { id: "mini-programs", name: "小程序", icon: "smartphone" },
];

// 难度等级数据
export const difficulties: { id: Difficulty; name: string; color: string }[] = [
  { id: "easy", name: "简单", color: "#10B981" },
  { id: "medium", name: "中等", color: "#F59E0B" },
  { id: "hard", name: "困难", color: "#EF4444" },
  { id: "expert", name: "专家", color: "#8B5CF6" },
];